<?php
namespace Modules\UI\Http\Controllers\Client\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use Setting;
use Validator;
use ThrowException;
use DB;
use Auth;

class UIController extends Controller
{
    // 此路由为用户分享app链接后进入的页面
    public function shareApp(Request $req)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $isOpeninstallEnabled =  @Setting::get('sys_share')['is_openinstall_enable'];

        $queryString = $_SERVER['QUERY_STRING'];

        // 没有开启openinstall安装
        if (!$isOpeninstallEnabled) {
            if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){

                $downloadKey = 'android_app_url';

            }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){

               $downloadKey = 'ios_app_url';

            }

            $downloadUrl = @Setting::get('sys_share')[$downloadKey];

            if (!$downloadUrl) {
                echo '未配置下载链接';
                exit();
            }

            return redirect($downloadUrl);
        }
        else {
            // 开启了openinstall安装。跳转到openinstall
            $url = env('APP_URL') . '/shareApp.html?appKey=' . @Setting::get('sys_share')['openinstall_appkey'] . '&' . $queryString;
            return redirect($url);
        }
    }
}
